﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4Shop.dao;
using MVC4Shop.Models;
using System.IO;

namespace MVC4Shop.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View(ProductDAO.GetAll());
        }

        //
        // GET: /Product/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            
               return View();
        }

        //
        // POST: /Product/Create
     
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection, ProductModel model, HttpPostedFileBase file)
        {
            String size = this.HttpContext.Session["size"].ToString();
            MemberModel userModel = (MemberModel)Session["user"];
            try
            {
                String path = Server.MapPath("~/" + userModel.username);
                if (Directory.Exists(path))
                {
                    String fileName = Path.GetFileName(file.FileName);
                    String path1 = Path.Combine(path, file.FileName);
                    file.SaveAs(path1);
                }
                else
                {
                    Directory.CreateDirectory(path);
                    String fileName = Path.GetFileName(file.FileName);
                    String path1 = Path.Combine(path, file.FileName);
                    file.SaveAs(path1);
                }
                // TODO: Add insert logic here
                //String category_id = collection["category_id"];
                //model.details = collection["elm1"].ToString();
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Product/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
